﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddPhysicalMachine : BasePage
    {
        public AssetDAL dal = new AssetDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindData();
            }
        }
         
        protected void BindData()
        {
            this.BindDropDownList();
            this.txtVirtualCenter.Visible = false;
            this.txtVMVersion.Visible = false;
            this.txtVMClusterName.Visible = false;
            this.txtBakServerName.Visible = false;
        }

        public void BindDropDownList()
        {
            this.ddlServerType.DataSource = dal.GetServerType();
            this.ddlServerType.DataTextField = "ServerType";
            this.ddlServerType.DataValueField = "ServerTypeID";
            this.ddlServerType.DataBind();

            this.ddlSiteCode.DataSource = dal.GetSiteCode();
            this.ddlSiteCode.DataTextField = "SiteCode";
            this.ddlSiteCode.DataValueField = "SiteCodeID";
            this.ddlSiteCode.DataBind();

            this.ddlRackName.DataSource = dal.GetRack();
            this.ddlRackName.DataTextField = "RackName";
            this.ddlRackName.DataValueField = "RackID";
            this.ddlRackName.DataBind();

            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlEnclosure.DataSource = dal.GetEnclosure();
            this.ddlEnclosure.DataTextField = "EnclosureName";
            this.ddlEnclosure.DataValueField = "EnclosureID";
            this.ddlEnclosure.DataBind();

            this.ddlPduName.DataSource = dal.GetPDU();
            this.ddlPduName.DataTextField = "PduName";
            this.ddlPduName.DataValueField = "PduID";
            this.ddlPduName.DataBind();

            this.ddlPduPort.DataSource = dal.GetPDU();
            this.ddlPduPort.DataTextField = "PduPort";
            this.ddlPduPort.DataValueField = "PduID";
            this.ddlPduPort.DataBind();

            this.ddlKvmName.DataSource = dal.GetKVM();
            this.ddlKvmName.DataTextField = "KvmName";
            this.ddlKvmName.DataValueField = "KvmID";
            this.ddlKvmName.DataBind();

            this.ddlVendorName.DataSource = dal.GetVendor();
            this.ddlVendorName.DataTextField = "VendorName";
            this.ddlVendorName.DataValueField = "VendorID";
            this.ddlVendorName.DataBind();
        }

        public void ddlServerType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ServerType = int.Parse(this.ddlServerType.SelectedValue.ToString());
            if (ServerType == 1)//Physical
            {
                this.txtVirtualCenter.Enabled = false;
                this.txtVirtualCenter.Visible = false;
                this.txtVMVersion.Enabled = false;
                this.txtVMVersion.Visible = false;
                this.txtVMClusterName.Enabled = false;
                this.txtVMClusterName.Visible = false;
                this.txtBakServerName.Enabled = false;
                this.txtBakServerName.Visible = false;
            }
            else if (ServerType == 2) //Virtual
            {
                this.txtVirtualCenter.Enabled = true;
                this.txtVirtualCenter.Visible = true;
                this.txtVMVersion.Enabled = true;
                this.txtVMVersion.Visible = true;
                this.txtVMClusterName.Enabled = true;
                this.txtVMClusterName.Visible = true;
                this.txtBakServerName.Enabled = true;
                this.txtBakServerName.Visible = true;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.PhysicalMachineInfoDataTable dt = new Asset.PhysicalMachineInfoDataTable();
            Asset.PhysicalMachineInfoRow rows = dt.NewPhysicalMachineInfoRow();
            Guid gd = new Guid();
            rows.PhysicalID = gd;
            rows.ServerTypeID = int.Parse(this.ddlServerType.SelectedValue.ToString());
            rows.AssetNo = this.txtAssetNo.Text;
            rows.UniqueNo = this.txtUniqueNo.Text;
            rows.Brand = this.txtBrand.Text;
            rows.Model = this.txtModel.Text;
            rows.SiteCodeID = int.Parse(this.ddlSiteCode.SelectedValue.ToString());
            rows.Floors = this.txtFloors.Text;
            rows.RackID = int.Parse(this.ddlRackName.SelectedValue.ToString());
            rows.PositionInRack = this.txtPositionInRack.Text;
            rows.iLOIP = this.txtiLOIP.Text;
            rows.CPU = this.txtCPU.Text;
            rows.RAM = this.txtRAM.Text;
            rows.HardDisk = this.txtHardDisk.Text;
            rows.StatusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.EnclosureID = int.Parse(this.ddlEnclosure.SelectedValue.ToString());
            rows.PduID = int.Parse(this.ddlPduName.SelectedValue.ToString());
            rows.PduID = int.Parse(this.ddlPduPort.SelectedValue.ToString());
            rows.KvmID = int.Parse(this.ddlKvmName.SelectedValue.ToString());
            rows.SN = this.txtSN.Text;
            rows.WarrantyStartDate = DateTime.Parse(this.txtWarrantyStartDate.Text);
            rows.WarrantyEndDate = DateTime.Parse(this.txtWarrantyEndDate.Text);
            rows.VendorID = int.Parse(this.ddlVendorName.SelectedValue.ToString());
            rows.VirtualCenter = this.txtVirtualCenter.Text;
            rows.VMVersion = this.txtVMVersion.Text;
            rows.VMClusterName = this.txtVMClusterName.Text;
            rows.BakServerName = this.txtBakServerName.Text;
            rows.Comments = this.txtComments.Text;

            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddPhysicalMachine(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("insert new PhysicalMachine named "+rows.AssetNo, LoginUser.UserName, ip);
                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
                Response.Redirect("AddPhysicalMachine.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");

            }


        }

    }
}
